﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑商品</title>
	<link rel="stylesheet" href="/css/bootstrap.min.css">
	<script src="/js/jquery/jquery.min.js"></script>
	<script src="/js/jquery.validation/1.14.0/jquery.validate.min.js"></script>
	<script src="/js/jquery.validation/1.14.0/messages_zh.min.js"></script>
	<style>
		input.error{
			border: 1px solid #E6594E;
		}
	</style>
</head>
<body>
    <div class="container">
	
		<form id="editGoodForm">
		  <br/>
		  <div class="form-group">
			<label for="gname">商品名称：</label>
			<input type="text" class="form-control" id="gname" name="gname" value="${goods.gname}" placeholder="输入名称">
		  </div>
		  <div class="form-group">
			<label for="gprice">商品价格：</label>
			<input type="text" class="form-control" id="gprice" name="gprice" value="${goods.gprice}" placeholder="输入价格">
		  </div>
		  <div class="form-group">
		  <button type="button" id="saveBtn" class="btn btn-success">提交</button>
		  <button type="button" id="cancelBtn" class="btn btn-default">取消</button>
		  </div>
		</form>
    </div>

		<script>
			var editGood = function(){
				if(!check().form()){ 
					return;  
				}
				$.ajax({
					   type: "GET",
					   dataType: "json",
					   url: "/goods/editGoods",
					   data: {
							"gid": ${goods.gid},
							"gname": $("#gname").val(),
							"gprice":$("#gprice").val()
					   },
					   success: function(msg){
							$('#cancelBtn').click();			
					   }
				});
			}
			
			$('#saveBtn').on('click', function(){
				editGood();
			});
			
			$('#cancelBtn').on('click', function(){
				var index = parent.layer.getFrameIndex(window.name);
				parent.getGoodsPageList();
				parent.layer.close(index);
			});

			
			//校验字段是否正确 
            function check(){ 
                /*返回一个validate对象，这个对象有一个form方法，返回的是是否通过验证*/ 
                return $("#editGoodForm").validate({ 
                            rules:{ 
                                gname:{ 
                                    required:true
                                },
                                gprice:{ 
                                    required:true                                   
                                }
                            }, 
                            messages:{ 
                                gname:{ 
                                    required:""
                                },
                                gprice:{ 
                                    required:""                                
                                }
                            }     
                        }); 
            } 
			
		</script> 	
</body>
</html>